package Test;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Accumulator {
    public static void main(String[] args) {

        final int[] sum1 = new int[10];                     // 声明数组存储每个线程的值
        int Sum = 0;                                        // 声明最终的总和变量

        Thread thread1 = new Thread(new Runnable() {        // 10条线程
            @Override
            public void run() {
                sum1[0] = 0;
                for (int i=1;i<=10;i++){
                    sum1[0] = sum1[0] + i;
                }
                System.out.println("1----"+sum1[0]);
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[1] = 0;
                for (int i=11;i<=20;i++){
                    sum1[1] = sum1[1] + i;
                }
                System.out.println("2----"+sum1[1]);
            }
        });
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[2] = 0;
                for (int i=21;i<=30;i++){
                    sum1[2] = sum1[2] + i;
                }
                System.out.println("3----"+sum1[2]);
            }
        });
        Thread thread4 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[3] = 0;
                for (int i=31;i<=40;i++){
                    sum1[3] = sum1[3] + i;
                }
                System.out.println("4----"+sum1[3]);
            }
        });
        Thread thread5 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[4] = 0;
                for (int i=41;i<=50;i++){
                    sum1[4] = sum1[4] + i;
                }
                System.out.println("5----"+sum1[4]);
            }
        });
        Thread thread6 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[5] = 0;
                for (int i=51;i<=60;i++){
                    sum1[5] = sum1[5] + i;
                }
                System.out.println("6----"+sum1[5]);
            }
        });
        Thread thread7 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[6] = 0;
                for (int i=61;i<=70;i++){
                    sum1[6] = sum1[6] + i;
                }
                System.out.println("7----"+sum1[6]);
            }
        });
        Thread thread8 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[7] = 0;
                for (int i=71;i<=80;i++){
                    sum1[7] = sum1[7] + i;
                }
                System.out.println("8----"+sum1[7]);
            }
        });
        Thread thread9 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[8] = 0;
                for (int i=81;i<=90;i++){
                    sum1[8] = sum1[8] + i;
                }
                System.out.println("9----"+sum1[8]);
            }
        });
        Thread thread10 = new Thread(new Runnable() {
            @Override
            public void run() {
                sum1[9] = 0;
                for (int i=91;i<=100;i++){
                    sum1[9] = sum1[9] + i;
                }
                System.out.println("10----"+sum1[9]);
            }
        });
        thread1.start();                                    // 启动线程
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();
        thread7.start();
        thread8.start();
        thread9.start();
        thread10.start();
        for (int i=0;i<sum1.length;i++){                    // 循环遍历数组
            try {
                thread1.join();                             // 线程插队，先执行每一部分的求和
                thread2.join();
                thread3.join();
                thread4.join();
                thread5.join();
                thread6.join();
                thread7.join();
                thread8.join();
                thread9.join();
                thread10.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Sum = Sum + sum1[i];                            // 通过遍历的元素求出总和
        }
        System.out.println("1-100的和为："+Sum);
    }
}
